package io.gitee.bluemiaomiao.dataanalyzebackend.service.impl;

import io.gitee.bluemiaomiao.dataanalyzebackend.domian.Ruser;
import io.gitee.bluemiaomiao.dataanalyzebackend.domian.RuserInfo;
import io.gitee.bluemiaomiao.dataanalyzebackend.repos.RuserDao;
import io.gitee.bluemiaomiao.dataanalyzebackend.repos.RuserRoleDao;
import io.gitee.bluemiaomiao.dataanalyzebackend.service.RuserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Ruser)表服务实现类
 *
 * @author makejava
 * @since 2020-11-07 16:09:24
 */
@Service("ruserService")
public class RuserServiceImpl implements RuserService {
  @Resource private RuserDao ruserDao;

  @Resource private RuserRoleDao ruserRoleDao;

  /**
   * 通过ID查询单条数据
   *
   * @param
   * @return 实例对象
   */
  @Override
  public Ruser queryById() {
    return this.ruserDao.queryById();
  }

  /**
   * 查询多条数据
   *
   * @param offset 查询起始位置
   * @param limit 查询条数
   * @return 对象列表
   */
  @Override
  public List<Ruser> queryAllByLimit(int offset, int limit) {
    return this.ruserDao.queryAllByLimit(offset, limit);
  }

  /**
   * 新增数据
   *
   * @param ruser 实例对象
   * @return 实例对象
   */
  @Override
  public Ruser insert(Ruser ruser) {
    ruser.setPassword(new BCryptPasswordEncoder(10).encode(ruser.getPassword()));
    this.ruserDao.insert(ruser);
    return ruser;
  }

  /**
   * 修改数据
   *
   * @param ruser 实例对象
   * @return 实例对象
   */
  @Override
  public int update(RuserInfo ruser) {
    System.out.println(ruser.getPassword());
    if (ruser.getPassword() == "" || ruser.getPassword() == null) {
    } else {
      ruser.setPassword(new BCryptPasswordEncoder(10).encode(ruser.getPassword()));
    }
    this.ruserDao.update(ruser);
    return 1;
  }

  /**
   * 通过主键删除数据
   *
   * @param
   * @return 是否成功
   */
  @Override
  public boolean deleteById(int id) {
    return this.ruserDao.deleteById(id) > 0;
  }

  @Override
  public Ruser queryByUsername(String username) {
    return this.ruserDao.queryByUsername(username);
  }

  @Override
  public List<RuserInfo> queryAll() {
    return this.ruserDao.queryAllRuser();
  };
}
